package managerBean;

import entity.ImageElementEntity;
import entity.TextElementEntity;
import entity.UnstructuredContentElementEntity;
import entity.UnstructuredContentEntity;
import entity.UnstructuredContentPageEntity;
import entity.UnstructuredContentPanelEntity;
import entity.VideoElementEntity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UIViewRoot;
import javax.faces.component.html.HtmlInputTextarea;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.StakeholderUnstructuredContentSessionBean;
import util.enumeration.Language;
//import javax.faces.component.html.HtmlInputTextarea;

/**
 *
 * @author WenYong
 */
@ManagedBean
@RequestScoped
public class StakeholderAddUnstructuredContentElementDetailsManager {

    private transient FacesContext context;
    private transient Application application;
    private transient ELContext elContext;
    private transient ExpressionFactory expressionFactory;
    private transient UIViewRoot view;
    private transient UIComponent panelOneGrid;
    private transient UIComponent panelTwoGrid;
    private transient UIComponent panelThreeGrid;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private List<UnstructuredContentPanelEntity> templatePanels;
    private static String textInputPanelOne;
    private static ArrayList textInputPanelOneList;
    private static String textInputPanelTwo;
    private static ArrayList textInputPanelTwoList;
    private static String textInputPanelThree;
    private static ArrayList textInputPanelThreeList;
    private int numOfImgElementsPanelOne;
    private int numOfImgElementsPanelTwo;
    private int numOfImgElementsPanelThree;
    private static int numOfImgUploadedPanelOne;
    private static int numOfImgUploadedPanelTwo;
    private static int numOfImgUploadedPanelThree;
    private int numOfVidUploadedPanelOne;
    private int numOfVidUploadedPanelTwo;
    private int numOfVidUploadedPanelThree;
    private static ArrayList imgPanelOneList;
    private static ArrayList imgPanelTwoList;
    private static ArrayList imgPanelThreeList;
    private static ArrayList vidPanelOneList;
    private static ArrayList vidPanelTwoList;
    private static ArrayList vidPanelThreeList;
    private static String videoPanelSelected;
    private int numOftxtUploadedPanelOne;
    private int numOftxtUploadedPanelTwo;
    private int numOftxtUploadedPanelThree;
    
    @EJB
    private StakeholderUnstructuredContentSessionBean unstructuredContentSession;

    /** Creates a new instance of StakeholderAddUnstructuredContentElementDetailsManager */
    public StakeholderAddUnstructuredContentElementDetailsManager() {
        if (imgPanelOneList == null) {
            imgPanelOneList = new ArrayList();
        }
        if (imgPanelTwoList == null) {
            imgPanelTwoList = new ArrayList();
        }
        if (imgPanelThreeList == null) {
            imgPanelThreeList = new ArrayList();
        }
        if (vidPanelOneList == null) {
            vidPanelOneList = new ArrayList();
        }
        if (vidPanelTwoList == null) {
            vidPanelTwoList = new ArrayList();
        }
        if (vidPanelThreeList == null) {
            vidPanelThreeList = new ArrayList();
        }
        if (textInputPanelOneList == null) {
            textInputPanelOneList = new ArrayList();
        }
        if (textInputPanelTwoList == null) {
            textInputPanelTwoList = new ArrayList();
        }
        if (textInputPanelThreeList == null) {
            textInputPanelThreeList = new ArrayList();
        }
        if (ctx.getExternalContext().getSessionMap().containsKey("templatePanels")) {
            templatePanels = (List<UnstructuredContentPanelEntity>) ctx.getExternalContext().getSessionMap().get("templatePanels");
        }
    }

    private void initContextMemebers() {
        context = FacesContext.getCurrentInstance();
        application = context.getApplication();
        elContext = context.getELContext();
        expressionFactory = application.getExpressionFactory();
        view = context.getViewRoot();
    }

    public UIComponent getPanelOneGrid() {
        if (panelOneGrid == null) {
            panelOneGrid = createPanelOne();
        }
        return panelOneGrid;
    }

    public void setPanelOneGrid(UIComponent panelOneGrid) {
        this.panelOneGrid = panelOneGrid;
    }

    private UIComponent createTextbox(String panel) {

        // create control
        String id = view.createUniqueId();
        UIComponent textbox = application.createComponent("javax.faces.HtmlInputTextarea");;
        textbox.setRendererType("javax.faces.Text");
        textbox.setId(id);
        textbox.setValueExpression("value", expressionFactory.createValueExpression(elContext, "#{stakeholderAddUnstructuredContentElementDetailsManager.textInput" + panel + "}", String.class));
        return textbox;
    }

    private UIComponent createImage(String panel, int num) {
        UIComponent image = application.createComponent("javax.faces.HtmlGraphicImage");
        String id = view.createUniqueId();
        image.setId(id);
        image.setRendererType("javax.faces.Image");
        Map<String, Object> attributes = image.getAttributes();
        if (panel.equalsIgnoreCase("PanelOne")) {
            if (!imgPanelOneList.isEmpty()) {
                attributes.put("value", "images/stakeholder/unstructured/" + imgPanelOneList.get(num).toString());
                attributes.put("width", "400px");
                attributes.put("height", "300px");
            }
        } else if (panel.equalsIgnoreCase("PanelTwo")) {
            if (!imgPanelTwoList.isEmpty()) {
                attributes.put("value", "images/stakeholder/unstructured/" + imgPanelTwoList.get(num).toString());
                attributes.put("width", "400px");
                attributes.put("height", "300px");
            }
        } else if (panel.equalsIgnoreCase("PanelThree")) {
            if (!imgPanelThreeList.isEmpty()) {
                attributes.put("value", "images/stakeholder/unstructured/" + panel + "_" + num + ".jpg");
                attributes.put("width", "400px");
                attributes.put("height", "300px");
            }
        }
        return image;
    }

    private UIComponent createCommandLink(String panel) {
        int num;
        String id = view.createUniqueId();
        UIComponent commandLink = application.createComponent("javax.faces.HtmlCommandLink");
        commandLink.setId(id);
        commandLink.setRendererType("javax.faces.Link");
        // set attributes (bind to printHello method)
        Map<String, Object> attributes = commandLink.getAttributes();
        MethodExpression action = expressionFactory.createMethodExpression(elContext,
                "#{stakeholderAddUnstructuredContentElementDetailsManager.imageload" + panel + "}",
                String.class, new Class<?>[0]);
        attributes.put("value", "Click here to upload image");
        //  attributes.put("onclick", "window.open('StakeholderAddUnstructuredContentUploadImage.xhtml', 'popupWindowName', 'dependent=yes, menubar=no, toolbar=no'); return false;"); 
        attributes.put("actionExpression", action);
        if (panel.equalsIgnoreCase("PanelOne")) {
            numOfImgElementsPanelOne++;
            num = numOfImgElementsPanelOne;
            imgPanelOneList.add(panel + "_" + num + ".jpg");
        } else if (panel.equalsIgnoreCase("PanelTwo")) {
            numOfImgElementsPanelTwo++;
            num = numOfImgElementsPanelTwo;
            imgPanelTwoList.add(panel + "_" + num + ".jpg");
        } else if (panel.equalsIgnoreCase("PanelThree")) {
            numOfImgElementsPanelThree++;
            num = numOfImgElementsPanelThree;
            imgPanelThreeList.add(panel + "_" + num + ".jpg");
        }
        return commandLink;
    }

    private UIComponent createVidCommandLink(String panel) {
        String vidPath;
        String id = view.createUniqueId();
        UIComponent commandLink = application.createComponent("javax.faces.HtmlCommandLink");
        commandLink.setId(id);
        commandLink.setRendererType("javax.faces.Link");
        String domain = "http://www.youtube.com/v/";
        Map<String, Object> attributes = commandLink.getAttributes();
        MethodExpression action = expressionFactory.createMethodExpression(elContext,
                "#{stakeholderAddUnstructuredContentElementDetailsManager.videoUpload" + panel + "}",
                String.class, new Class<?>[0]);
        attributes.put("value", "Click here link a YouTube video");
        //  attributes.put("onclick", "window.open('StakeholderAddUnstructuredContentUploadImage.xhtml', 'popupWindowName', 'dependent=yes, menubar=no, toolbar=no'); return false;"); 
        attributes.put("actionExpression", action);
        if ((ctx.getExternalContext().getSessionMap().containsKey("videoName")) && videoPanelSelected.equalsIgnoreCase("PanelOne")) {
            vidPath = ctx.getExternalContext().getSessionMap().get("videoName").toString();
            int posOfAnd = vidPath.indexOf("&");
            int equal = vidPath.indexOf("=");
            equal = equal + 1;
            vidPath = vidPath.substring(equal, posOfAnd);
            vidPath = domain + vidPath;
            System.out.println("VideoPath: " + vidPath);
            vidPanelOneList.add(vidPath);
        } else if ((ctx.getExternalContext().getSessionMap().containsKey("videoName")) && videoPanelSelected.equalsIgnoreCase("PanelTwo")) {
            vidPath = ctx.getExternalContext().getSessionMap().get("videoName").toString();
            int posOfAnd = vidPath.indexOf("&");
            int equal = vidPath.indexOf("=");
            equal = equal + 1;
            vidPath = vidPath.substring(equal, posOfAnd);
            vidPath = domain + vidPath;
            vidPanelTwoList.add(vidPath);
        } else if ((ctx.getExternalContext().getSessionMap().containsKey("videoName")) && videoPanelSelected.equalsIgnoreCase("PanelThree")) {
            vidPath = ctx.getExternalContext().getSessionMap().get("videoName").toString();
            int posOfAnd = vidPath.indexOf("&");
            int equal = vidPath.indexOf("=");
            equal = equal + 1;
            vidPath = vidPath.substring(equal, posOfAnd);
            vidPath = domain + vidPath;
            vidPanelThreeList.add(vidPath);
        }
        return commandLink;
    }

    private UIComponent createMedia(String panel, int num) {
        String vidPath = "";
        String id = view.createUniqueId();
        UIComponent label = application.createComponent("javax.faces.HtmlOutputLabel");
        label.setId(id);
        label.setRendererType("javax.faces.Label");
        if (panel.equalsIgnoreCase("PanelOne")) {
            if (!vidPanelOneList.isEmpty()) {
                System.out.println("Video in panel one is not empty");
                vidPath = vidPanelOneList.get(num).toString();
                System.out.println("VideoPathLoaded 1 : " + vidPath);
                Map<String, Object> attributes = label.getAttributes();
                attributes.put("value", "<object width='420' height='315'><param name='movie' value='" + vidPath + "?version=3&amp;hl=en_US&amp;rel=0'></param>"
                        + "<param name='allowFullScreen' value='true'></param>"
                        + "<param name='allowscriptaccess' value='always'></param>"
                        + "<embed src='" + vidPath + "?version=3&amp;hl=en_US&amp;rel=0' type='application/x-shockwave-flash' width='420' height='315' allowscriptaccess='always' allowfullscreen='true'></embed>"
                        + "</object>");
                System.out.println("VideoPathLoaded 2 : " + vidPath);
                attributes.put("escape", false);
            }
        }
        if (panel.equalsIgnoreCase("PanelTwo")) {
            if (!vidPanelTwoList.isEmpty()) {
                vidPath = vidPanelTwoList.get(num).toString();
                System.out.println("Video in panel two is not empty");
                Map<String, Object> attributes = label.getAttributes();
                attributes.put("value", "<object width='420' height='315'><param name='movie' value='" + vidPath + "?version=3&amp;hl=en_US&amp;rel=0'></param>"
                        + "<param name='allowFullScreen' value='true'></param>"
                        + "<param name='allowscriptaccess' value='always'></param>"
                        + "<embed src='" + vidPath + "?version=3&amp;hl=en_US&amp;rel=0' type='application/x-shockwave-flash' width='420' height='315' allowscriptaccess='always' allowfullscreen='true'></embed>"
                        + "</object>");
                attributes.put("escape", false);
            }
        }
        if (panel.equalsIgnoreCase("PanelThree")) {
            if (!vidPanelThreeList.isEmpty()) {
                System.out.println("Video in panel three is not empty");
                vidPath = vidPanelThreeList.get(num).toString();
                Map<String, Object> attributes = label.getAttributes();
                attributes.put("value", "<object width='420' height='315'><param name='movie' value='" + vidPath + "?version=3&amp;hl=en_US&amp;rel=0'></param>"
                        + "<param name='allowFullScreen' value='true'></param>"
                        + "<param name='allowscriptaccess' value='always'></param>"
                        + "<embed src='" + vidPath + "?version=3&amp;hl=en_US&amp;rel=0' type='application/x-shockwave-flash' width='420' height='315' allowscriptaccess='always' allowfullscreen='true'></embed>"
                        + "</object>");
                attributes.put("escape", false);
            }
        }
        return label;
    }

    private UIComponent createStartBreak() {
        String id = view.createUniqueId();
        UIComponent label = application.createComponent("javax.faces.HtmlOutputLabel");
        label.setId(id);
        label.setRendererType("javax.faces.Label");
        Map<String, Object> attributes = label.getAttributes();
        attributes.put("value", "<p>");
        attributes.put("escape", false);
        return label;
    }

    private UIComponent createEndBreak() {
        String id = view.createUniqueId();
        UIComponent label = application.createComponent("javax.faces.HtmlOutputLabel");
        label.setId(id);
        label.setRendererType("javax.faces.Label");
        Map<String, Object> attributes = label.getAttributes();
        attributes.put("value", "</p>");
        attributes.put("escape", false);
        return label;
    }

    private UIComponent createPanelOne() {
        initContextMemebers();
        UIComponent panelGrid = application.createComponent("javax.faces.HtmlPanelGroup");
        String id = view.createUniqueId();
        panelGrid.setId(id);
        panelGrid.setRendererType("javax.faces.Group");
        for (int i = 0; i < templatePanels.size(); i++) {
            if (templatePanels.get(i).getPanelName().equalsIgnoreCase("CONTENT1")) {
                List<UnstructuredContentElementEntity> elementList = templatePanels.get(i).getUnstructuredContentElement();
                Collections.sort(elementList, new Comparator<UnstructuredContentElementEntity>() {

                    @Override
                    public int compare(UnstructuredContentElementEntity ce1, UnstructuredContentElementEntity ce2) {
                        return ce1.getDisplayOrder().compareTo(ce2.getDisplayOrder());
                    }
                });
                for (int j = 0; j < elementList.size(); j++) {
                    if (elementList.get(j).getName().contains("txt")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent textbox = createTextbox("PanelOne");
                        panelGrid.getChildren().add(textbox);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                    } else if (elementList.get(j).getName().contains("img")) {

                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent commandLink = createCommandLink("PanelOne");
                        panelGrid.getChildren().add(commandLink);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                        UIComponent labe2 = createStartBreak();
                        panelGrid.getChildren().add(labe2);
                        UIComponent image = createImage("PanelOne", numOfImgUploadedPanelOne);
                        panelGrid.getChildren().add(image);
                        UIComponent labe3 = createEndBreak();
                        panelGrid.getChildren().add(labe3);
                    } else if (elementList.get(j).getName().contains("vid")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent commandLink = createVidCommandLink("PanelOne");
                        panelGrid.getChildren().add(commandLink);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                        UIComponent labe2 = createStartBreak();
                        panelGrid.getChildren().add(labe2);
                        UIComponent link = createMedia("PanelOne", numOfVidUploadedPanelOne);
                        panelGrid.getChildren().add(link);
                        UIComponent labe3 = createEndBreak();
                        panelGrid.getChildren().add(labe3);
                        numOfVidUploadedPanelOne++;
                    }
                }

            }
        }
        return panelGrid;
    }

    private UIComponent createPanelTwo() {
        initContextMemebers();
        UIComponent panelGrid = application.createComponent("javax.faces.HtmlPanelGroup");
        String id = view.createUniqueId();
        panelGrid.setId(id);
        panelGrid.setRendererType("javax.faces.Group");
        for (int i = 0; i < templatePanels.size(); i++) {
            if (templatePanels.get(i).getPanelName().equalsIgnoreCase("CONTENT2")) {
                List<UnstructuredContentElementEntity> elementList = templatePanels.get(i).getUnstructuredContentElement();
                Collections.sort(elementList, new Comparator<UnstructuredContentElementEntity>() {

                    @Override
                    public int compare(UnstructuredContentElementEntity ce1, UnstructuredContentElementEntity ce2) {
                        return ce1.getDisplayOrder().compareTo(ce2.getDisplayOrder());
                    }
                });
                for (int j = 0; j < elementList.size(); j++) {
                    if (elementList.get(j).getName().contains("txt")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent textbox = createTextbox("PanelTwo");
                        panelGrid.getChildren().add(textbox);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                    } else if (elementList.get(j).getName().contains("img")) {

                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent commandLink = createCommandLink("PanelTwo");
                        panelGrid.getChildren().add(commandLink);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                        UIComponent labe2 = createStartBreak();
                        panelGrid.getChildren().add(labe2);
                        UIComponent image = createImage("PanelTwo", numOfImgUploadedPanelTwo);
                        panelGrid.getChildren().add(image);
                        UIComponent labe3 = createEndBreak();
                        panelGrid.getChildren().add(labe3);
                    } else if (elementList.get(j).getName().contains("vid")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent commandLink = createVidCommandLink("PanelTwo");
                        panelGrid.getChildren().add(commandLink);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                        UIComponent labe2 = createStartBreak();
                        panelGrid.getChildren().add(labe2);
                        UIComponent link = createMedia("PanelTwo", numOfVidUploadedPanelTwo);
                        panelGrid.getChildren().add(link);
                        UIComponent labe3 = createEndBreak();
                        panelGrid.getChildren().add(labe3);
                        numOfVidUploadedPanelTwo++;

                    }
                }
            }
        }
        return panelGrid;
    }

    private UIComponent createPanelThree() {
        initContextMemebers();
        UIComponent panelGrid = application.createComponent("javax.faces.HtmlPanelGroup");
        String id = view.createUniqueId();
        panelGrid.setId(id);
        panelGrid.setRendererType("javax.faces.Group");
        for (int i = 0; i < templatePanels.size(); i++) {
            if (templatePanels.get(i).getPanelName().equalsIgnoreCase("CONTENT3")) {
                List<UnstructuredContentElementEntity> elementList = templatePanels.get(i).getUnstructuredContentElement();
                Collections.sort(elementList, new Comparator<UnstructuredContentElementEntity>() {

                    @Override
                    public int compare(UnstructuredContentElementEntity ce1, UnstructuredContentElementEntity ce2) {
                        return ce1.getDisplayOrder().compareTo(ce2.getDisplayOrder());
                    }
                });
                for (int j = 0; j < elementList.size(); j++) {
                    if (elementList.get(j).getName().contains("txt")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent textbox = createTextbox("PanelThree");
                        panelGrid.getChildren().add(textbox);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                    } else if (elementList.get(j).getName().contains("img")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent commandLink = createCommandLink("PanelThree");
                        panelGrid.getChildren().add(commandLink);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                        UIComponent labe2 = createStartBreak();
                        panelGrid.getChildren().add(labe2);
                        UIComponent image = createImage("PanelThree", numOfImgUploadedPanelThree);
                        panelGrid.getChildren().add(image);
                        UIComponent labe3 = createEndBreak();
                        panelGrid.getChildren().add(labe3);
                    } else if (elementList.get(j).getName().contains("vid")) {
                        UIComponent label = createStartBreak();
                        panelGrid.getChildren().add(label);
                        UIComponent commandLink = createVidCommandLink("PanelThree");
                        panelGrid.getChildren().add(commandLink);
                        UIComponent label1 = createEndBreak();
                        panelGrid.getChildren().add(label1);
                        UIComponent labe2 = createStartBreak();
                        panelGrid.getChildren().add(labe2);
                        UIComponent link = createMedia("PanelThree", numOfVidUploadedPanelThree);
                        panelGrid.getChildren().add(link);
                        UIComponent labe3 = createEndBreak();
                        panelGrid.getChildren().add(labe3);
                        numOfVidUploadedPanelOne++;
                    }
                }
            }
        }
        return panelGrid;
    }

    public void initView(PhaseEvent event) {
    }

    public UIComponent getPanelTwoGrid() {
        if (panelTwoGrid == null) {
            panelTwoGrid = createPanelTwo();
        }
        return panelTwoGrid;
    }

    public void setPanelTwoGrid(UIComponent panelTwoGrid) {
        this.panelTwoGrid = panelTwoGrid;
    }

    public UIComponent getPanelThreeGrid() {
        if (panelThreeGrid == null) {
            panelThreeGrid = createPanelThree();
        }
        return panelThreeGrid;
    }

    public void setPanelThreeGrid(UIComponent panelThreeGrid) {
        this.panelThreeGrid = panelThreeGrid;
    }

    public String getTextInputPanelOne() {
        //numOftxtUploadedPanelOne--;
        System.out.println("Get panel One");
        if (!textInputPanelOneList.isEmpty()) {
            textInputPanelOne = textInputPanelOneList.get(numOftxtUploadedPanelOne).toString();
            System.out.println("Text in Panel One: " + textInputPanelOne);
            numOftxtUploadedPanelOne++;
        }
        return textInputPanelOne;
    }

    public void setTextInputPanelOne(String textInputPanelOne) {
        System.out.println("Set Panel one");
        textInputPanelOneList.add(textInputPanelOne);
        System.out.println("Setting Text in Panel One: " + textInputPanelOne);
        numOftxtUploadedPanelOne--;
        this.textInputPanelOne = textInputPanelOne;
    }

    public String getTextInputPanelTwo() {
        if (!textInputPanelTwoList.isEmpty()) {
            System.out.println("Panel 2 size: "+ textInputPanelTwoList.size());
            textInputPanelTwo = textInputPanelTwoList.get(numOftxtUploadedPanelTwo).toString();
            System.out.println("Text Stored in Panel Two: " +  textInputPanelTwoList.get(numOftxtUploadedPanelTwo).toString());
            numOftxtUploadedPanelTwo++;
        }
        return textInputPanelTwo;
    }

    public void setTextInputPanelTwo(String textInputPanelTwo) {
        textInputPanelTwoList.add(textInputPanelTwo);
        System.out.println("Text Stored in Panel Two: "+textInputPanelTwo);
         numOftxtUploadedPanelTwo--;
        this.textInputPanelTwo = textInputPanelTwo;
    }

    public String getTextInputPanelThree() {
        if (!textInputPanelThreeList.isEmpty()) {
            textInputPanelThree = textInputPanelThreeList.get(numOftxtUploadedPanelThree).toString();
            numOftxtUploadedPanelThree++;
        }
        return textInputPanelThree;
    }

    public void setTextInputPanelThree(String textInputPanelThree) {
        textInputPanelThreeList.add(textInputPanelThree);
        numOftxtUploadedPanelThree--;
        this.textInputPanelThree = textInputPanelThree;
    }

    public void imageloadPanelOne() {
        try {
            System.out.println("Number: " + numOfImgUploadedPanelOne);
            String fileName = imgPanelOneList.get(numOfImgUploadedPanelOne).toString();
            ctx.getExternalContext().getSessionMap().put("Panel", fileName);
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentUploadImage.xhtml");
            numOfImgUploadedPanelOne++;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void imageloadPanelTwo() {
        try {
            if (!imgPanelTwoList.isEmpty()) {
                String fileName = imgPanelTwoList.get(numOfImgUploadedPanelTwo).toString();
                ctx.getExternalContext().getSessionMap().put("Panel", fileName);
                String serverName = ctx.getExternalContext().getRequestServerName();
                String serverPort = "8080";
                String contextPath = ctx.getExternalContext().getRequestContextPath();
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentUploadImage.xhtml");
                numOfImgUploadedPanelTwo++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void imageloadPanelThree() {
        try {
            if (!imgPanelThreeList.isEmpty()) {
                String fileName = imgPanelThreeList.get(numOfImgUploadedPanelThree).toString();
                ctx.getExternalContext().getSessionMap().put("Panel", fileName);
                String serverName = ctx.getExternalContext().getRequestServerName();
                String serverPort = "8080";
                String contextPath = ctx.getExternalContext().getRequestContextPath();
                ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentUploadImage.xhtml");
                numOfImgUploadedPanelThree++;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void videoUploadPanelOne() {
        try {
            videoPanelSelected = "PanelOne";
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentUploadVideo.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void videoUploadPanelTwo() {
        try {
            videoPanelSelected = "PanelTwo";
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentUploadVideo.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void videoUploadPanelThree() {
        try {
            videoPanelSelected = "PanelTwo";
            String serverName = ctx.getExternalContext().getRequestServerName();
            String serverPort = "8080";
            String contextPath = ctx.getExternalContext().getRequestContextPath();
            ctx.getExternalContext().redirect("http://" + serverName + ":" + serverPort + contextPath + "/StakeholderAddUnstructuredContentUploadVideo.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void confirmDetails() {
        for (int i = 0; i < templatePanels.size(); i++) {
            if (templatePanels.get(i).getPanelName().equalsIgnoreCase("CONTENT1")) {
                int textAdded = 0;
                int imgAdded = 0;
                int vidAdded = 0;
                List<UnstructuredContentElementEntity> elementList = templatePanels.get(i).getUnstructuredContentElement();
                Collections.sort(elementList, new Comparator<UnstructuredContentElementEntity>() {

                    @Override
                    public int compare(UnstructuredContentElementEntity ce1, UnstructuredContentElementEntity ce2) {
                        return ce1.getDisplayOrder().compareTo(ce2.getDisplayOrder());
                    }
                });
                for (int j = 0; j < elementList.size(); j++) {
                    if (elementList.get(j).getName().contains("txt")) {
                        String desc = textInputPanelOneList.get(textAdded).toString();
                        TextElementEntity e = (TextElementEntity) elementList.get(j);
                        e.setText(desc);
                        textAdded++;
                    } else if (elementList.get(j).getName().contains("img")) {
                        String url = imgPanelOneList.get(imgAdded).toString();
                        ImageElementEntity img = (ImageElementEntity) elementList.get(j);
                        img.setImageLink(url);
                        imgAdded++;
                    } else if (elementList.get(j).getName().contains("vid")) {
                        String vidurl = vidPanelOneList.get(vidAdded).toString();
                        VideoElementEntity vid = (VideoElementEntity) elementList.get(j);
                        vid.setVideoLink(vidurl);
                        vidAdded++;
                    }
                }
            }
            if (templatePanels.get(i).getPanelName().equalsIgnoreCase("CONTENT2")) {
                int textAdded = 0;
                int imgAdded = 0;
                int vidAdded = 0;
                List<UnstructuredContentElementEntity> elementList = templatePanels.get(i).getUnstructuredContentElement();
                Collections.sort(elementList, new Comparator<UnstructuredContentElementEntity>() {

                    @Override
                    public int compare(UnstructuredContentElementEntity ce1, UnstructuredContentElementEntity ce2) {
                        return ce1.getDisplayOrder().compareTo(ce2.getDisplayOrder());
                    }
                });
                for (int j = 0; j < elementList.size(); j++) {
                    if (elementList.get(j).getName().contains("txt")) {
                        String desc = textInputPanelTwoList.get(textAdded).toString();
                        TextElementEntity e = (TextElementEntity) elementList.get(j);
                        e.setText(desc);
                        textAdded++;
                    } else if (elementList.get(j).getName().contains("img")) {
                        String url = imgPanelTwoList.get(imgAdded).toString();
                        ImageElementEntity img = (ImageElementEntity) elementList.get(j);
                        img.setImageLink(url);
                        imgAdded++;
                    } else if (elementList.get(j).getName().contains("vid")) {
                        String vidurl = vidPanelTwoList.get(vidAdded).toString();
                        VideoElementEntity vid = (VideoElementEntity) elementList.get(j);
                        vid.setVideoLink(vidurl);
                        vidAdded++;
                    }
                }
            }
            if (templatePanels.get(i).getPanelName().equalsIgnoreCase("CONTENT3")) {
                int textAdded = 0;
                int imgAdded = 0;
                int vidAdded = 0;
                List<UnstructuredContentElementEntity> elementList = templatePanels.get(i).getUnstructuredContentElement();
                Collections.sort(elementList, new Comparator<UnstructuredContentElementEntity>() {

                    @Override
                    public int compare(UnstructuredContentElementEntity ce1, UnstructuredContentElementEntity ce2) {
                        return ce1.getDisplayOrder().compareTo(ce2.getDisplayOrder());
                    }
                });
                for (int j = 0; j < elementList.size(); j++) {
                    if (elementList.get(j).getName().contains("txt")) {
                        String desc = textInputPanelThreeList.get(textAdded).toString();
                        TextElementEntity e = (TextElementEntity) elementList.get(j);
                        e.setText(desc);
                        textAdded++;
                    } else if (elementList.get(j).getName().contains("img")) {
                        String url = imgPanelThreeList.get(imgAdded).toString();
                        ImageElementEntity img = (ImageElementEntity) elementList.get(j);
                        img.setImageLink(url);
                        imgAdded++;
                    } else if (elementList.get(j).getName().contains("vid")) {
                        String vidurl = vidPanelThreeList.get(vidAdded).toString();
                        VideoElementEntity vid = (VideoElementEntity) elementList.get(j);
                        vid.setVideoLink(vidurl);
                        vidAdded++;
                    }
                }
            }
        }
        int templateNo = Integer.parseInt(ctx.getExternalContext().getSessionMap().get("TemplateSelected").toString());
        UnstructuredContentPageEntity contentPage = new UnstructuredContentPageEntity(templateNo,Language.ENGLISH,templatePanels);
        UnstructuredContentEntity cont = new UnstructuredContentEntity();
        cont.getUnstructuredContentPage().add(contentPage);
        boolean result = unstructuredContentSession.addContent(cont);
        if(result == true)
        {
             FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Content has been added successfully"));
        }
        else
        {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Content could not be added due to some technical errors"));
        }
        
    }
}
